package dao;

import org.apache.commons.dbutils.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;

public class AccountDao {
    private static final String TABLE_NAME = "account";
    public boolean transfer(Connection connection, String from, String to, double money) throws Exception {
        String sqlIn = "update `" + TABLE_NAME + "` set balance = balance + ? where id = ?";
        String sqlOut = "update `" + TABLE_NAME + "` set balance = balance - ? where id = ?";
        QueryRunner runner = new QueryRunner();
        int rowOut = runner.update(connection, sqlOut, money, from);
        int rowIn = runner.update(connection, sqlIn, money, from);
        return rowOut > 0 && rowIn > 0;
    }
}
